package b4.config
{
	public class LayerConfig
	{
		public var boundX_Min:int;
		public var boundX_Max:int;
		public var boundY_Min:int;
		public var boundY_Max:int;
		
		public var boundXAY_Min:int;
		public var boundXAY_Max:int;
		public var boundXMY_Min:int;
		public var boundXMY_Max:int;
		
		public var map:Object;
		public var items:Array;
		
		public function LayerConfig(layerStr:String)
		{
			map = new Object();
			items = new Array();
			var layerStrs:Array = layerStr.split("\n");
			for (var i:int = 0; i < layerStrs.length; i++)
			{
				var itemStrs:Array = layerStrs[i].split(",");
				if (itemStrs.length > 1)
				{
					var type:int = parseInt(itemStrs[0]);
					var itemConfig:LayerItemConfig = createLayerItem(type, itemStrs);
					itemConfig.index = items.length;
					items.push(itemConfig);
					for (var posStr:String in itemConfig.occPoints)
					{
						var x:int = posStr.split(" ")[0];
						var y:int = posStr.split(" ")[1];
						setMapItem(x, y, itemConfig);
					} 
				}
			}
		}
		
		public function setMapItem(x:int, y:int, itemConfig:LayerItemConfig):void
		{
			map[x + " " + y] = itemConfig;
			if (x < boundX_Min)
				boundX_Min = x;
			else if (x > boundX_Max)
				boundX_Max = x;
			if (y < boundY_Min)
				boundY_Min = y;
			else if (y > boundY_Max)
				boundY_Max = y;
			if (x + y < boundXAY_Min)
				boundXAY_Min = x + y;
			else if (x + y > boundXAY_Max)
				boundXAY_Max = x + y;
			if (x - y < boundXMY_Min)
				boundXMY_Min = x - y;
			else if (x - y > boundXMY_Max)
				boundXMY_Max = x - y;
		}
		
		public static function createLayerItem(type:int, itemStrs:Array = null):LayerItemConfig
		{
			var layerItem:LayerItemConfig;
			switch (type)
			{
				case LayerItemConfig.TYPE_PORT:
					layerItem = new LayerItemConfigPort(itemStrs);
				break;
				case LayerItemConfig.TYPE_GATE:
					layerItem = new LayerItemConfigGate(itemStrs);
				break;
				case LayerItemConfig.TYPE_MAZE_OBJECT:
					layerItem = new LayerItemConfigMazeObj(itemStrs);
				break;
				case LayerItemConfig.TYPE_TREASURE_BOX:
					layerItem = new LayerItemConfigTreasureBox(itemStrs);
				break;
				case LayerItemConfig.TYPE_CREATURE:
					layerItem = new LayerItemConfigCreature(itemStrs);
				break;
				default:
					layerItem = new LayerItemConfig(itemStrs);
				break;
			}
			return layerItem;
		}

	}
}